package edu.contoller;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.annotations.LoginRequired;
import edu.common.CommonResult;
import edu.common.Verification;
import edu.entity.Do.User;
import edu.enumeration.RedisPathEnum;
import edu.mapper.UserMapper;
import edu.service.UserService;
import edu.utils.JwtUtil;
import edu.utils.RedisUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/user")
@CrossOrigin(origins = "*",maxAge = 3600)
public class UserController {

    @Resource
    UserService userService;

    @Resource
    RedisUtil redisUtil;

    @Resource
    UserMapper userMapper;


    @RequestMapping(value = "/sendVerificationCode",method = RequestMethod.GET)
    public CommonResult<Long> sendVerificationCode(String phone) {
        Verification verification = userService.sendVerificationCode(phone);
        if (verification.getStatus().equals(200)) {
            return new CommonResult<>(200);
        }else if (verification.getStatus().equals(201)) {
            return new CommonResult<>(201);
        }else {
            return new CommonResult<>(202,verification.getTime());
        }
    }


    @RequestMapping(value = "/doLogin",method = RequestMethod.GET)
    public CommonResult<String> doLogin(String phone, String verificationCode) {


        User user = userMapper.selectOne(
                new QueryWrapper<User>()
                        .eq("phone", phone)
                        .last("limit 1")
        );

        if (user == null) {
            user = new User();
            user.setPhone(phone);
            user.setBalance(200.0);
            userMapper.insert(user);
        }

        Map<String, String> map = new HashMap<>();
        map.put("id",String.valueOf(user.getId()));
        String token = JwtUtil.create(map);

        return new CommonResult<>(204,token);
//        if (!redisUtil.hasKey(RedisPathEnum.VERIFICATION.getPath() + phone)) {
//            return new CommonResult<>(205);
//        }else if (verificationCode.equals(redisUtil.get(RedisPathEnum.VERIFICATION.getPath() + phone) + "")) {
//
//            Integer count = userMapper.selectCount(
//                    new QueryWrapper<User>()
//                            .eq("phone", phone)
//                            .last("limit 1")
//            );
//            if (count.equals(0)) {
//                User user = new User();
//                user.setPhone(phone);
//                userMapper.insert(user);
//            }
//
//            User user = userMapper.selectOne(
//                    new QueryWrapper<User>()
//                            .eq("phone", phone)
//                            .last("limit 1")
//            );
//
//            Map<String, String> map = new HashMap<>();
//            map.put("id",String.valueOf(user.getId()));
//            String token = JwtUtil.create(map);
//
//            return new CommonResult<>(204,token);
//        }else {
//            return new CommonResult<>(205);
//        }
    }


    @LoginRequired
    @RequestMapping(value = "/getUserInfoById",method = RequestMethod.GET)
    public User getUserInfoById(HttpServletRequest request) {
        Integer id = Integer.parseInt(JwtUtil.getClaims(request.getHeader("token")).get("id").asString());
        return userMapper.selectById(id);
    }









    @LoginRequired
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String hello() {
        return "success";
    }

}
